import hashlib
import time
import requests

sess = requests.session()
ip = "192.168.1.59"
def cookie_test():
    # 发起登录请求
    data = {"userName":"admin","password":"1234"}
    rp = sess.request("post","http://"+ip+":8080/pinter/bank/api/login",data=data)
    print(rp.text)
    #发起查询请求
    # sess 全局变量帮我们自动处理了cookie
    params =  {"userName":"admin"}
    rp = sess.request("get","http://"+ip+":8080/pinter/bank/api/query",params=params)
    print(rp.text)
# token 鉴权
# 函数注释 行间注释 添加日志 代码解释 调优建议
def token_test():
    # 登录后获得权利
    data = {"userName":"admin","password":"1234"}
    rp = sess.request("post", "http://" + ip + ":8080/pinter/bank/api/login2", data=data)
    print(rp.text)
    # print(type(rp.json()))
    mytoken = rp.json().get("data") #鉴权接口的 请求头中添加一个属性
    # print(mytoken)
    # 鉴定它有没有权利
    params = {"userName":"admin"}
    headers = {"testfan-token": mytoken}
    rp = sess.request("get", "http://" + ip + ":8080/pinter/bank/api/query2",params = params, headers = headers)
    print(rp.text)

# 签名验证
# 1.辅助模块:hashlib 加密方法
            #m = hashlib.md5 ("123456".encode())   $%$%^&*&*((()()*234454555555
            # m. hexdigest() 获取加密结果
            # time time.time()  时问戳
# python的 时间戳,js 语言的时间戳 长度不一样。int(time.time()*1000)

# 函数注释 行间注释 添加日志 生成单测 代码解释 调优建议
def sign_test():
    # 生成时间戳
    mytime = int(time.time()*1000)
    # 生成签名认证
    mysige = hashlib.md5(("18434795728" + "testfan" + str(mytime)).encode())
    print(mysige.hexdigest())
    json = {"phoneNum":"18434795728","optCode":"testfan","timestamp":mytime,"sign":mysige.hexdigest()}
    rp = sess.request( "post", "http://" + ip + ":8080/pinter/com/userInfo",json = json)
    print(rp.text)
# 辅助模块：
if __name__ == '__main__':
    cookie_test()
    token_test()
    sign_test()